<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>绘制文字</title>
    <script type="text/javascript">
        function load () {
            let c = document.getElementById('myCanvas');
            let context = c.getContext('2d');
            
            let txt = '练习';

            context.font = 'italic 35px 黑体';

            // 实心字
            context.fillStyle = '#ff0000';
            context.fillText('练习', 30,  60);
            
            // 字体位置
            // textAlign 是相对于(30, 100)这个点的 缺省是left center, start, end
            // context.textAlign = 'center';

            // 中线的位置 默认是底部
            // context.textBaseline = 'top'; // top, middle, bottom

            // let width =  parseInt(String(context.measureText(txt)/3));
            let width = context.measureText(txt).width;
            console.log(width);

            // 空心字
            context.strokeStyle = '#00ff00';
            context.strokeText(txt, width - width/3, 110);
        }

        window.onload  = function () {
            load();
        }
    </script>
</head>
<body>
    <canvas id="myCanvas" style="border: 1px solid #000000" width="800" height="450">
        你的浏览器不支持canvas元素！
    </canvas>
</body>
</html>